//
//  TableViewCellOfChannelAccess.m
//  UI_homework_22_QQ音乐界面和阿里星球界面
//
//  Created by li cheng on 16/8/13.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "TableViewCellOfChannelAccess.h"
#import "CollectionViewCellOfChannelAccess.h"
#import "RootModel.h"
@interface TableViewCellOfChannelAccess()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) RootModel *rootModel;
@end
@implementation TableViewCellOfChannelAccess
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        [self.contentView addSubview:_collectionView];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[CollectionViewCellOfChannelAccess class] forCellWithReuseIdentifier:@"CollectionViewCellOfChannelAccess"];
    }
    return self;
}
#pragma mark - 数据解析
- (void)hangdleData{
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    urlRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            self.rootModel = [RootModel modelWithDictionary:dic];
        });
    }];
    [dataTask resume];
    
}

#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
    CGFloat itemSizeHeight = self.contentView.bounds.size.height;
    CGFloat itemSizeWidth = itemSizeHeight;
    _layout.itemSize = CGSizeMake(itemSizeWidth, itemSizeHeight);
    _layout.minimumLineSpacing = 0;
    CGFloat edge = 40;
    CGFloat minimumInteritemSpacingValue = (self.contentView.bounds.size.width - 3 * itemSizeWidth - 2 * edge) / 2;
    _layout.minimumInteritemSpacing = minimumInteritemSpacingValue;
    _layout.sectionInset = UIEdgeInsetsMake(0, edge, 0, edge);
}

#pragma mark - item 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCellOfChannelAccess *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCellOfChannelAccess" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor magentaColor];

    return cell;
}
#pragma mark - 其他
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
